<template>
  <div class="login-container">
    <div class="logo" />
    <div class="form">
      <h1>智慧园区-登录</h1>

      <el-card shadow="never" class="login-card">
        <!--登录表单-->
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules">
          <el-form-item prop="username" label="账号">
            <el-input v-model="loginForm.username" placeholder="请输入用户名" />
          </el-form-item>

          <el-form-item prop="password" label="密码">
            <el-input v-model="loginForm.password" placeholder="请输入密码" show-password />
          </el-form-item>

          <el-form-item prop="uuid" label="模型uuid：">
            <el-input v-model="loginForm.uuid" placeholder="请输入验证码" type="password" show-password />
          </el-form-item>

          <el-form-item prop="isAgree">
            <el-checkbox v-model="loginForm.isAgree">
              remember me
            </el-checkbox>
          </el-form-item>

          <el-form-item>
            <el-button style="width:350px" type="primary" @click="onLogin">登录</el-button>
          </el-form-item>
        </el-form>
      </el-card>

    </div>

  </div>

</template>

<script>
import store from '@/store/index.js'
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: store.state.user.username,
        password: store.state.user.password,
        uuid: store.state.user.uuid,
        isAgree: true
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 4, max: 16, message: '用户名长度应在4-16位之间', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '密码长度应在6-16位之间', trigger: 'blur' }
        ],
        uuid: [
          { required: true, message: '请输入uuid', trigger: 'blur' }
        ]
      },
      redirect: ''
    }
  },
  created() {
    this.redirect = this.$route.query.redirect
  },
  methods: {
    async onLogin() {
      if (this.loginForm.isAgree) {
        store.commit('user/SET_INFO', {
          username: this.loginForm.username,
          password: this.loginForm.password,
          uuid: this.loginForm.uuid
        })
      } else {
        store.commit('user/SET_INFO', { username: '', password: '', uuid: '' })
      }
      // 非空校验
      await this.$refs.loginForm.validate()
      await store.dispatch('user/loginAction', {
        username: this.loginForm.username,
        password: this.loginForm.password
      })
      this.$message({
        message: '登录成功',
        type: 'success'
      })
      this.$router.push(this.redirect ? this.redirect : '/')
    }
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  width: 100vw;
  height: 100vh;
  display: flex;
  flex-direction: row;
  justify-content: left;
    .logo{
      flex: 3;
      background-image: url(../../assets/common/login_back.jpg);
      background-size: cover;
      background-position: center;
      border-top-right-radius: 36px;
    }
    .form{
       flex: 2;
       display: flex;
       flex-direction: column;
       justify-content: center;
       align-items: center;
    }
}
</style>
